/*     */ package net.sourceforge.appgen.wizard;
/*     */ 
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.lang.reflect.InvocationTargetException;
/*     */ import net.sourceforge.appgen.model.MappingData;
/*     */ import net.sourceforge.appgen.xml.XmlData;
/*     */ import net.sourceforge.appgen.xml.XmlDataException;
/*     */ import org.eclipse.core.resources.IContainer;
/*     */ import org.eclipse.core.resources.IFile;
/*     */ import org.eclipse.core.resources.IResource;
/*     */ import org.eclipse.core.resources.IWorkspace;
/*     */ import org.eclipse.core.resources.IWorkspaceRoot;
/*     */ import org.eclipse.core.resources.ResourcesPlugin;
/*     */ import org.eclipse.core.runtime.CoreException;
/*     */ import org.eclipse.core.runtime.IProgressMonitor;
/*     */ import org.eclipse.core.runtime.IStatus;
/*     */ import org.eclipse.core.runtime.Path;
/*     */ import org.eclipse.core.runtime.Status;
/*     */ import org.eclipse.jface.dialogs.MessageDialog;
/*     */ import org.eclipse.jface.operation.IRunnableWithProgress;
/*     */ import org.eclipse.jface.viewers.ISelection;
/*     */ import org.eclipse.jface.viewers.IStructuredSelection;
/*     */ import org.eclipse.jface.wizard.IWizardContainer;
/*     */ import org.eclipse.jface.wizard.Wizard;
/*     */ import org.eclipse.swt.widgets.Display;
/*     */ import org.eclipse.swt.widgets.Shell;
/*     */ import org.eclipse.ui.INewWizard;
/*     */ import org.eclipse.ui.IWorkbench;
/*     */ import org.eclipse.ui.IWorkbenchPage;
/*     */ import org.eclipse.ui.IWorkbenchWindow;
/*     */ import org.eclipse.ui.PartInitException;
/*     */ import org.eclipse.ui.PlatformUI;
/*     */ import org.eclipse.ui.ide.IDE;
/*     */ 
/*     */ public class MappingDataWizard extends Wizard
/*     */   implements INewWizard
/*     */ {
/*     */   private MappingDataPage page;
/*     */   private ISelection selection;
/*     */ 
/*     */   public MappingDataWizard()
/*     */   {
/*  61 */     setNeedsProgressMonitor(true);
/*  62 */     setWindowTitle("New AppGen Mapping Data File");
/*     */   }
/*     */ 
/*     */   public void addPages()
/*     */   {
/*  67 */     this.page = new MappingDataPage(this.selection);
/*  68 */     addPage(this.page);
/*     */   }
/*     */ 
/*     */   public boolean performFinish()
/*     */   {
/*  73 */     final String containerName = this.page.getContainerName();
/*  74 */     final String fileName = this.page.getFileName();
/*  75 */     IRunnableWithProgress op = new IRunnableWithProgress() {
/*     */       public void run(IProgressMonitor monitor) throws InvocationTargetException {
/*     */         try {
/*  78 */           MappingDataWizard.this.doFinish(containerName, fileName, monitor);
/*     */         } catch (CoreException e) {
/*  80 */           throw new InvocationTargetException(e);
/*     */         } finally {
/*  82 */           monitor.done();
/*     */         }
/*     */       } } ;
/*     */     try {
/*  87 */       getContainer().run(true, false, op);
/*     */     } catch (InterruptedException localInterruptedException) {
/*  89 */       return false;
/*     */     } catch (InvocationTargetException e) {
/*  91 */       Throwable realException = e.getTargetException();
/*  92 */       MessageDialog.openError(getShell(), "Error - InvocationTargetException", realException.getMessage());
/*  93 */       return false;
/*     */     }
/*  95 */     return true;
/*     */   }
/*     */ 
/*     */   private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
/*  99 */     monitor.beginTask("Creating " + fileName, 2);
/*     */ 
/* 101 */     IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
/* 102 */     IResource resource = root.findMember(new Path(containerName));
/*     */ 
/* 104 */     if ((!resource.exists()) || (!(resource instanceof IContainer))) {
/* 105 */       throwCoreException("Container \"" + containerName + "\" does not exist.");
/*     */     }
/* 107 */     IContainer container = (IContainer)resource;
/* 108 */     final IFile file = container.getFile(new Path(fileName));
/*     */ 
/* 110 */     MappingData mappingData = new MappingData();
/* 111 */     XmlData xmlData = new XmlData(mappingData);
/*     */     try
/*     */     {
/* 114 */       String xmlText = xmlData.getXmlText();
/* 115 */       InputStream in = new ByteArrayInputStream(xmlText.getBytes());
/* 116 */       file.create(in, true, monitor);
/* 117 */       in.close();
/*     */     } catch (XmlDataException e) {
/* 119 */       MessageDialog.openError(getShell(), "Error - XmlDataException", e.getMessage());
/*     */     } catch (IOException e) {
/* 121 */       MessageDialog.openError(getShell(), "Error - IOException", e.getMessage());
/*     */     }
/*     */ 
/* 124 */     monitor.worked(1);
/*     */ 
/* 126 */     monitor.setTaskName("Opening file for editing...");
/*     */ 
/* 128 */     getShell().getDisplay().asyncExec(new Runnable() {
/*     */       public void run() {
/* 130 */         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
/*     */         try
/*     */         {
/* 133 */           IDE.openEditor(page, file, "net.sourceforge.appgen.editor.MappingDataEditor", true);
/*     */         } catch (PartInitException e) {
/* 135 */           MessageDialog.openError(MappingDataWizard.this.getShell(), "Error - PartInitException", e.getMessage());
/*     */         }
/*     */ 
/* 138 */         file.getParent().getWorkspace().getSynchronizer().notifyAll();
/*     */       }
/*     */     });
/* 142 */     monitor.worked(1);
/*     */   }
/*     */ 
/*     */   private void throwCoreException(String message) throws CoreException {
/* 146 */     IStatus status = new Status(4, "net.sourceforge.appgen", 0, message, null);
/* 147 */     throw new CoreException(status);
/*     */   }
/*     */ 
/*     */   public void init(IWorkbench workbench, IStructuredSelection selection) {
/* 151 */     this.selection = selection;
/*     */   }
/*     */ }

/* Location:           C:\Users\lzl6066\Downloads\net.sourceforge.appgen_1.2.0\plugins\net.sourceforge.appgen_1.2.0.201002031718\
 * Qualified Name:     net.sourceforge.appgen.wizard.MappingDataWizard
 * JD-Core Version:    0.6.0
 */